package g14.sc12project.milestone1.BookStore;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import g14.sc12project.milestone1.Common.Constants;
import g14.sc12project.milestone1.Common.Utils;
import g14.sc12project.milestone1.Common.XMLParser;

public class BookCategory implements BookCategoryWS {
	
	XMLParser parser=null;
	Document doc = null;
	
	
	public BookCategory(String XMLDBFileName){
		parser = new XMLParser();
		doc = parser.loadXMLDoc(XMLDBFileName);
	}

	/* 
	 * @see SC12Project.Milestone1.BookStore.BookCategoryWS#getAllBooKNames()
	 */
	@Override
	public List<String> getAllBooKNames() {
		// xpath query
		String selectQuery = Constants.GET_ALL_BOOK_NAMES;
		NodeList nodes = parser.getNodesByQuery(doc, selectQuery);
		List<String> bookNames = new ArrayList<String>();
		for (int count = 0; count < nodes.getLength(); count++) {
			String title = nodes.item(count).getTextContent();
			bookNames.add(title);
		}
		
		return bookNames;
	}

	@Override
	public List<String> getAllBookISBN10() {
		//xpath query
		String selectQuery = Constants.GET_ALL_BOOK_ISBN10;
		NodeList nodes = parser.getNodesByQuery(doc, selectQuery);
		List<String> ISBN10s = new ArrayList<String>();
		for (int count = 0; count < nodes.getLength(); count++) {
			String ISBN10 = nodes.item(count).getTextContent();
			ISBN10s.add(ISBN10);
		}
		
		return ISBN10s;
	}

	@Override
	public List<String> getAllBookISBN13() {
		//xpath query
		String selectQuery = Constants.GET_ALL_BOOK_ISBN13;
		NodeList nodes = parser.getNodesByQuery(doc, selectQuery);
		List<String> ISBN13s = new ArrayList<String>();
		for (int count = 0; count < nodes.getLength(); count++) {
			String ISBN13 = nodes.item(count).getTextContent();
			ISBN13s.add(ISBN13);
		}
		
		return ISBN13s;
	}

	@Override
	public List<BookInfo> getBooksByTitle(String title) {
		
		String selectQuery = "category/book[title='"+title+"']/title";
		NodeList nodes = parser.getNodesByQuery(doc, selectQuery);
		List<BookInfo> books = new ArrayList<BookInfo>();
		for (int count = 0; count < nodes.getLength(); count++) {
			Node node = nodes.item(count);
			Node bookNode = node.getParentNode();
			
			Utils util=new Utils();
			BookInfo book= util.getBookInfo(bookNode);
			books.add(book);
		}
		
		return books;
	}

	@Override
	public List<BookInfo> getBooksByAuthor(String author) {
		String selectQuery = "category/book/authors[author='"+author+"']/author";
		NodeList nodes = parser.getNodesByQuery(doc, selectQuery);
		List<BookInfo> books = new ArrayList<BookInfo>();
		for (int count = 0; count < nodes.getLength(); count++) {
			Node node = nodes.item(count);
			Node authors = node.getParentNode();
			Node bookNode = authors.getParentNode();
			
			Utils util=new Utils();
			BookInfo book= util.getBookInfo(bookNode);
			books.add(book);
		}
		
		return books;
	}

	@Override
	public BookInfo getBookInfobyISBN10(String isbn10) {
		String selectQuery = "category/book[ISBN10='"+isbn10+"']/ISBN10";
		NodeList nodes = parser.getNodesByQuery(doc, selectQuery);
		BookInfo book = null;
		for (int count = 0; count < nodes.getLength(); ) {
			Node node = nodes.item(count);
			Node bookNode = node.getParentNode();
			
			Utils util=new Utils();
			book= util.getBookInfo(bookNode);
			break;
		}
		
		return book;
	}

	@Override
	public BookInfo getBookInfobyISBN13(String isbn13) {
		String selectQuery = "category/book[ISBN13='"+isbn13+"']/ISBN13";
		NodeList nodes = parser.getNodesByQuery(doc, selectQuery);
		BookInfo book = null;
		for (int count = 0; count < nodes.getLength(); ) {
			Node node = nodes.item(count);
			Node bookNode = node.getParentNode();
			
			Utils util=new Utils();
			book= util.getBookInfo(bookNode);
			break;
		}
		
		return book;
	}

	

}
